<?php

!defined('IN_ROOT') && exit('Access Denied');

class sitemodel {

    var $db;
    var $base;

    function sitemodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }


    function get($id) {
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."site WHERE id='$id'");
    }

    function get_list($start=0,$limit=1000) {
        $sitelist=array();
        $query=$this->db->query("SELECT * FROM `".DB_TABLEPRE."site` ORDER BY `displayorder` ASC,`id` ASC limit $start,$limit");
        while($site=$this->db->fetch_array($query)) {
        	$site[areaname] = $this->db->result_first("SELECT name FROM `".DB_TABLEPRE."area` where id='$site[aid]' ");
            $sitelist[]=$site;
        }
        return $sitelist;
    }
    function get_list_select($id) {
        $str = '<select name="sid">';
        $query=$this->db->query("SELECT id,name FROM `".DB_TABLEPRE."site` ORDER BY `id` ASC");
        while($v=$this->db->fetch_array($query)) {
        	if($id == $v['id'])
					$select = 'selected';
				else 
					$select = '';
        	$str .= '<option value="' . $v['id'] . '" ' . $select . '>'.$v['name'].'</option>';
        }
        return $str.'</select>';
    }
    function get_list_checkbox($id) {
        $query=$this->db->query("SELECT id,name FROM `".DB_TABLEPRE."site` ORDER BY `id` ASC");
        $ids = explode(",",$id);
        while($v=$this->db->fetch_array($query)) {
        	if(in_array($v['id'],$ids))
					$select = 'checked';
				else 
					$select = '';
        	$str .= '<input type="checkbox" name="sids[' . $v['id'] . ']" value="' . $v['id'] . '" ' . $select . '>'.$v['name'].'&nbsp;&nbsp;';
        }
        return $str;
    }


    function add($aid,$dir,$ifopen=0,$name,$py,$domain,$keywords,$descs,$tpls,$displayorder=0,$available=1) {
        $this->db->query('REPLACE INTO `'.DB_TABLEPRE."site`(`aid` ,`dir` ,`ifopen` ,`name` ,`pinyin` ,`domain` ,`keywords` ,`descs` ,`tpls` ,`displayorder` ,`available`) values ('$aid','$dir','$ifopen','$name','$py','$domain','$keywords','$descs','$tpls','$displayorder','$available')");
        return $this->db->insert_id();
    }

    function update($aid,$dir,$ifopen=0,$name,$py,$domain,$keywords,$descs,$tpls,$available=1,$id) {
        //var_dump($ifopen);exit;
        $this->db->query('UPDATE  `'.DB_TABLEPRE."site`  set `aid`='$aid',`dir`='$dir',`ifopen`=$ifopen,`name`='$name',`pinyin`='$py',`domain`='$domain',`keywords`='$keywords',`descs`='$descs',`tpls`='$tpls',`available`='$available' where id=$id ");
    }

    function remove_by_id($ids) {
        $this->db->query("DELETE FROM `".DB_TABLEPRE."site` WHERE `id` IN ($ids)");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."map` WHERE `sid` IN ($ids)");
    }
    function order_site($id,$order) {
        $this->db->query("UPDATE `".DB_TABLEPRE."site` SET 	`displayorder` = '{$order}' WHERE `id` = '{$id}'");
    }
    function update_available($id,$available) {
        $this->db->query("UPDATE `".DB_TABLEPRE."site` SET 	`available` = '{$available}' WHERE `id` = '{$id}'");
    }

}
?>
